<?php
namespace Seller\Controller;
use Think\Controller;

class LoginController extends Controller
{
    //后台管理员的页面和登录处理
    public function index()
    {
        if (IS_POST)
        {
            $where["mobile"] = trim(I('post.name'));
            $manager = M('manager')->where($where)->find();
            //店铺是否审核通过
            $status = M('shop')->where(array("id"=>$manager['shop_id']))->getField('state');
            if($status != 1){
                $this->error("该店铺还未通过审核" ,$_SERVER['HTTP_REFERER'] ,1);
            }

            $frozen = M('shop')->where(array("id"=>$manager['shop_id']))->getField('frozen');
            if ($frozen == 1) {
                $this->error("该店铺已经被冻结");
            }


            if($manager) {
                if($manager["m_status"] != 1){
                    $this->error("您的账号已被关闭" ,$_SERVER['HTTP_REFERER'] ,1);
                }
                $pwd = trim(I('post.password'));
                $password = md5(md5($pwd) .$manager["salt"] );

                if($password != $manager["password"]){
                    $this->error("密码错误" ,$_SERVER['HTTP_REFERER'] ,1);
                }
                $role = D("RoleView")->where("manager.id = %d" ,array($manager["id"]))->find();
                if(!$role){
                    $this->error("管理员未给您分配角色" ,$_SERVER['HTTP_REFERER'] ,1);
                }
                // 登陆成功
                session('manager_id' ,$manager['id']);
                session('role_id' ,$role['role_id']);
                session('avatar' ,$role['avatar']);
                session('name' ,$role['user_name']);
                session('mobile' ,$role['mobile']);
                session("role_name" ,$role['role_name']);
                session("shop_id",$manager['shop_id']);
                M("manager")->where("id = ".$manager["id"])->save(array("last_login" => time(),"last_ip" => get_client_ip()));
                $this->redirect('Index/index');
            }else{
                $this->error("用户名错误" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        } else {
            $shop_id = session('shop_id');
            if ($shop_id > 0){
                $this->redirect('Index/index');
            }
            $this->display();
        }
    }

    // 清除 runtime 缓存
    public function clearcache()
    {
        deldir ( RUNTIME_PATH );
        $this->ajaxReturn(array("status" => "success"));
    }

    //管理员退出登录
    public function logout()
    {
        session('manager_id' ,null);
        session('role_id' ,null);
        session('avatar' ,null);
        session('name' ,null);
        session('mobile' ,null);
        session("role_name" ,null);
        session("shop_id",null);
        $this->success("退出成功",U('Login/index'));
//        $this->redirect('Login/index');
    }

    public function register(){
        $this->shop = D('Shop');
        if (IS_POST) {
            //用户账号【也就是手机is_numeric
            $phone = to_int(I("post.phone"));
            if (!isMobile($phone)) {
                $this->error('请填写正确的手机号码!', $_SERVER['HTTP_REFERER'], 1);
            }
//            //验证重复性
            $res = M('shop')->where(array('mobile' => $phone))->find();
            $ret = M('manager')->where(array('mobile'=>$phone))->find();
            if (!empty($res)||!empty($ret)) {
                $this->error("已有该手机号进行注册过", $_SERVER['HTTP_REFERER'], 1);
            }
            $shop_name = I("post.shop_name");
            if (empty($shop_name)) {
                $this->error("店铺名称必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $company = I("post.company");
            if (empty($company)) {
                $this->error("公司名称必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $tel = trim(I("post.tel"));
            if (!islandline($tel)) {
                $this->error("固定电话格式不正确！", $_SERVER['HTTP_REFERER'], 1);
            }
            $address = I("post.address");
            if (empty($address)) {
                $this->error("店铺地址必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $bank_no = trim(I("post.bank_no"));
            if (empty($bank_no)||!is_numeric($bank_no)) {
                $this->error("请正确填写银行卡号", $_SERVER['HTTP_REFERER'], 1);
            }
            $bank_user_name = I("post.bank_user_name");
            if (empty($bank_user_name)) {
                $this->error("银行卡号所有人必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $license_img = I("post.license_img");
            if (empty($license_img)) {
                $this->error("营业执照必需上传！", $_SERVER['HTTP_REFERER'], 1);
            }
            $create_desc = I("post.create_desc");
            if (empty($create_desc)) {
                $this->error("开店说明必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $shop_name = I("post.shop_name");
            if (empty($shop_name)) {
                $this->error("店铺名称必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $data_one = array(
                'shop_name' => I('post.shop_name'),
                'company' => I('post.company'),
                'tel' => I('post.tel'),
                'address' => I('post.address'),
                'bank_no' => I('post.bank_no'),
                'bank_user_name' => I('post.bank_user_name'),
                'create_desc' => I('post.create_desc'),
                'state' => I('post.state'),
                'license_img' => I('post.license_img'),
                'create_time' => time()
            );
            $bool1 = $this->shop->add($data_one);
            $shop_id = M('shop')->getLastInsId();
            $keeper = I("post.keeper");
            if (!$keeper) {
                $this->error("店主必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }

            $abc = I("post.password");
            $salt = substr(md5(time() . $phone), -8);
            $password = md5(md5(I('post.password')) . $salt);
            $data_two = array(
                'shop_id' => $shop_id,
                'name' => $keeper,
                'mobile' => $phone,
                'salt' => $salt,
                'password' => $password
            );
            $bool2 = M('manager')->add($data_two);
            $manager_id = M('manager')->getLastInsId();

            if ($bool1 && $bool2 == true) {
//                //超管角色
            M('role_manager')->add(array('manager_id' => $manager_id, 'role_id' => "1"));
//                $role = D("RoleView")->where("manager.id = %d", array($manager_id))->find();
//                session('manager_id', $manager_id);
//                session('role_id', $role['role_id']);
//                session('avatar', $role['avatar']);
//                session('name', $role['user_name']);
//                session('mobile', $role['mobile']);
//                session("role_name", $role['role_name']);
//                session("shop_id", $shop_id);
//                M("manager")->where("id = ".$manager_id)->save(array("last_login" => time(),"last_ip" => get_client_ip()));
                $this->success('新增成功', U('Login/index'));
            } else {
                $this->error('提交失败!', $_SERVER['HTTP_REFERER'], 1);
            }
        };
    }

    public function createShop(){
        $this->display();
    }
}